<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>头像选择</title>
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.7.0/dist/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.min.js"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f9;
            margin: 0;
            padding: 20px;
        }

        .main {
            max-width: 600px;
            margin: 0 auto;
            background-color: white;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            text-align: center;
        }

        .avatar-grid {
            display: flex;
            flex-wrap: wrap;
            justify-content: center;
            gap: 10px;
        }

        .avatar-item img {
            width: 60px;
            height: 60px;
            object-fit: cover;
            border: 2px solid transparent;
            cursor: pointer;
        }

        .selected img {
            border-color: #007bff;
        }

        .user-avatar {
            width: 100px;
            height: 100px;
            object-fit: cover;
            border-radius: 50%;
            margin-bottom: 20px;
        }

        .submit-btn {
            margin-top: 20px;
        }
    </style>
</head>
<body>
<div class="main">
    <div class="current-avatar">
        <img :src="selectedAvatarUrl" alt="Selected Avatar" class="user-avatar">
    </div>
    <div class="avatar-grid">
        <div v-for="n in avatarCount" :key="n" @click="selectAvatar(n)" class="avatar-item" :class="{ selected: n === selectedAvatarId }">
            <img :src="'img/head/' + n + '.jpg'" alt="Avatar">
        </div>
    </div>
    <button @click="submitAvatar" class="btn btn-primary submit-btn">提交</button>
</div>

<script>
    new Vue({
        el: '.main',
        data: {
            avatarCount: 11,
            selectedAvatarId: null,
            selectedAvatarUrl: ''
        },
        methods: {
            selectAvatar(id) {
                this.selectedAvatarId = id;
                this.selectedAvatarUrl = 'img/head/' + id + '.jpg';
            },
            submitAvatar() {
                console.log('Submitting avatar...');
                if (this.selectedAvatarId) {
                    // 定义 head 变量并赋值为选中的头像名称
                    const head = this.selectedAvatarId + '.jpg';
                    console.log(head);

                    $.ajax({
                        url: "updateStudentHead/"+head,
                        type: "post",
                        contentType: "json", // 添加此行以确保发送JSON数据
                        success: function (response) {
                            alert('头像提交成功');
                        },
                        error: function (error) {
                            console.error('Error submitting avatar:', error);
                            alert('头像提交失败，请重试');
                        }
                    });
                } else {
                    alert('请选择一个头像');
                }
            }
        }
    });
</script>
</body>
</html>